package com.imooc.service.impl;

import com.github.pagehelper.PageHelper;
import com.imooc.base.BaseInfoProperties;
import com.imooc.base.RabbitMqConfig;
import com.imooc.entity.mo.MessageMO;
import com.imooc.entity.pojo.Fans;
import com.imooc.entity.vo.FansVo;
import com.imooc.entity.vo.VlogerVo;
import com.imooc.enums.MessageEnum;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.FansMapper;
import com.imooc.mapper.FansMapperCustom;
import com.imooc.service.FansService;
import com.imooc.service.MsgService;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FansServiceImpl extends BaseInfoProperties implements FansService {
    @Autowired
    private FansMapper fansManager;
    @Autowired
    private Sid sid;
    @Autowired
    private FansMapperCustom fansMapperCustom;
    @Autowired
    private MsgService msgService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    @Transactional
    public void doFollow(String myId, String vlogerId) {
        Fans fans = new Fans();
        fans.setId(sid.nextShort());
        fans.setFanId(myId);
        fans.setVlogerId(vlogerId);
        Fans hasRel = queryByFanIdAndVlogerId(vlogerId, myId);
        if(hasRel!=null){
            hasRel.setIsFanFriendOfMine(1);
            fansManager.updateRel(hasRel);
            fans.setIsFanFriendOfMine(1);
        }else{
            fans.setIsFanFriendOfMine(0);
        }
        fansManager.saveRel(fans);
        // 系统消息: 关注
//        msgService.createMsg(myId,vlogerId, MessageEnum.FOLLOW_YOU.type,null);
        MessageMO messageMO = new MessageMO();
        messageMO.setFromUserId(myId);
        messageMO.setToUserId(vlogerId);
        messageMO.setCreateTime(new Date());
        messageMO.setMsgContent(null);
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME,"sys.msg."+MessageEnum.FOLLOW_YOU.enValue, JsonUtils.objectToJson(messageMO));
    }

    @Override
    public void doCancel(String fanId, String vlogerId) {
        // 查询和对方的关系
        Fans fansRel = queryByFanIdAndVlogerId(vlogerId, fanId);
        // 如果为朋友关系 删除关系并更新对方关系
        if(fansRel!=null){


            if(fansRel.getIsFanFriendOfMine()== YesOrNo.YES.type){
                Fans vlogerRel = queryByFanIdAndVlogerId(fanId, vlogerId);
                vlogerRel.setIsFanFriendOfMine(YesOrNo.NO.type);
                fansManager.updateRel(vlogerRel);
            }
            fansManager.deleteRel(fansRel);

        }else{
            fansRel = fansManager.findByFanIdAndVlogerId(fanId, vlogerId);
            log.info("没有关系取消关注:{}",fansRel);
            // 如果不是朋友关系 则直接删除关系
            fansManager.deleteRel(fansRel);
        }

    }

    @Override
    public boolean queryDoIFollowVloger(String myId, String vlogerId) {
        return queryByFanIdAndVlogerId(myId,vlogerId)!=null;
    }

    @Override
    public PagedGridResult queryMyFollow(String myId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<VlogerVo> vlogerVos = fansMapperCustom.queryMyFollow(myId);
        return setterPagedGrid(vlogerVos,page);
    }

    @Override
    public PagedGridResult queryMyFans(String myId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<FansVo> vlogerVos = fansMapperCustom.queryMyFans(myId);
        for (FansVo f : vlogerVos) {
            String relationship = redis.get(REDIS_USER_AND_VLOGGER_RELATIONSHIP + ":" + myId + ":" + f.getFanId());
            if(StringUtils.isNotBlank(relationship)&&relationship.equalsIgnoreCase("1")){
                f.setFriend(true);
            }
        }
        return setterPagedGrid(vlogerVos,page);
    }

    /**
     * 查询双方关系
     * @param fanId vlogerId
     * @param vlogerId
     * @return
     */
    private Fans queryByFanIdAndVlogerId(String fanId,String vlogerId){
        return fansManager.findByFanIdAndVlogerId(fanId,vlogerId);
    }
}
